package com.app;


import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.app.bean.ClassmateDetailBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class ClassmateDetailActivity extends AppCompatActivity {

    private Button btnBack;
    private TextView tvEmail;
    private TextView tvPhone;
    private TextView tvClass;
    private TextView tvName;
    private TextView tvUserId;
    private ImageView ivBack;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classmate_detail);
        Intent intent = getIntent();
        id = intent.getIntExtra("id",0);
        Log.e("AAA", "onCreate: "+id);
        initView();
        initEvent();
        initData();
    }

    private void initEvent() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ClassmateDetailActivity.this,ClassmateListActivity.class));
            }
        });
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击返回按钮，跳转界面
                startActivity(new Intent(ClassmateDetailActivity.this,ClassmateListActivity.class));
            }
        });
    }

    private void initData() {
        String url = "http://www.xhjjyst.com/freshmenapp/classmate/findone";
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        JSONObject object = new JSONObject();
        try {
            object.put("id",id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                Log.e("AAA", "onResponse: "+jsonObject);
                ClassmateDetailBean classmateDetailBean = gson.fromJson(jsonObject.toString(), ClassmateDetailBean.class);

                    ClassmateDetailBean.DataBean data = classmateDetailBean.getData();
                    setData(data);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(ClassmateDetailActivity.this, "请求错误"+volleyError, Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(request);
    }

    private void setData(ClassmateDetailBean.DataBean data) {
        tvName.setText("姓名        "+data.getName());
        tvClass.setText("班级        "+data.getClassName());
        tvEmail.setText("邮箱        "+data.getEmail());
        tvPhone.setText("电话        "+data.getTelephone());
        tvUserId.setText("编号        "+data.getId());
    }

    private void initView() {
        ivBack = findViewById(R.id.iv_back);
        tvUserId = findViewById(R.id.tv_user_id);
        tvName = findViewById(R.id.tv_name);
        tvClass = findViewById(R.id.tv_class);
        tvPhone = findViewById(R.id.tv_phone);
        tvEmail = findViewById(R.id.tv_email);
        btnBack = findViewById(R.id.btn_back);
    }
}